package cn.zmit.kuxi.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.entity.OldShareEntity;
import cn.zmit.kuxi.holder.OldShareHolder;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;
import com.xdroid.widget.loadmore.LoadMoreContainer;
import com.xdroid.widget.loadmore.LoadMoreHandler;
import com.xdroid.widget.loadmore.LoadMoreListViewContainer;

/** * @author @parameter * @return */
public class OldShareActivity extends BaseActivity {
	@ViewInject(R.id.listView_old_announce)
	private ListView listView_old_announce;
	private ListViewDataAdapter<OldShareEntity> oldShareAdapter;
	private String goodsId;
	/**
	 * 页码
	 */
	private int mPage = 1;
	@ViewInject(R.id.load_more_list_view_container)
	private LoadMoreListViewContainer mLoadMoreListViewContainer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("往期晒单", true);
		setContentView(R.layout.activity_old_announced);
		ViewUtils.inject(this);
		goodsId = getTextFromBundle("goods_id");
		initView();
		getData();
		initLoadMore();
	}

	/**
	 * 初始化上拉刷新，分页显示
	 */
	private void initLoadMore() {
		mLoadMoreListViewContainer.useDefaultFooter(); // 使用默认的FooterView
		mLoadMoreListViewContainer.setLoadMoreHandler(new LoadMoreHandler() {

			@Override
			public void onLoadMore(LoadMoreContainer loadMoreContainer) {
				if (!mLoadMoreListViewContainer.isLoadError()) {
					mPage++;
				}
				getData();
			}
		});
	}

	private void getData() {
		RequestTask.getInstance().getOldShare(this, goodsId, mPage + "",
				new onRequestListener());
	}

	// 初始化view
	private void initView() {
		oldShareAdapter = new ListViewDataAdapter<OldShareEntity>();
		oldShareAdapter.setViewHolderClass(this, OldShareHolder.class);
		listView_old_announce.setAdapter(oldShareAdapter);
		OldShareHolder
				.setOnListItemSelectListener(new OnListItemSelectListener<OldShareEntity>() {

					@Override
					public void onlistItemSelect(int position,
							OldShareEntity itemData) {
						Intent intent = new Intent();
						Bundle bundle = new Bundle();
						bundle.putString("flag", "往期晒单");
						bundle.putString("evaluate_id",
								itemData.getEvaluate_id());
						bundle.putString("title", itemData.getTitle());
						bundle.putString("goodsName", itemData.getGoods_name());
						bundle.putString("winUser", itemData.getPhone());
						bundle.putString("content", itemData.getEvaluate());
						bundle.putStringArrayList("imageUrl",
								(ArrayList<String>) itemData
										.getImageURLEntities());
						intent.putExtras(bundle);
						intent.setClass(OldShareActivity.this,
								ShareDetailsActivity.class);
						startActivity(intent);

					}
				});
	}

	// 请求
	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			Boolean mIsEmpty = false; // 是否是空数据
			Boolean mHasMore = false; // 是否有更多数据待加载
			JsonData jsonData = JsonData.create(response);
			JsonData data = jsonData.optJson("evaluate");
			String goodsName = jsonData.optString("goods_name");
			if (data != null && data.length() > 0) {
				if (data.length() > 0) {
					if (data.length() == 8) {
						mHasMore = true; // 有更多数据
					} else if (data.length() < 8) {
						mIsEmpty = false; // 有数据
						mHasMore = false; // 无更多数据
					}
					for (int i = 0; i < data.length(); i++) {
						JsonData itemData = data.optJson(i);
						String evaluate_id = itemData.optString("evaluate_id");
						String title = itemData.optString("phase");
						String content = itemData.optString("content");
						String time = itemData.optString("evaluateOn");
						String phone = itemData.optString("phone");
						List<String> mPicList = new ArrayList<String>();// 初始化图片路径
						String pictures = itemData.optString("pictures");
						if (!pictures.equals("")) {
							String[] pic = pictures.split("\\|");
							for (int j = 0; j < pic.length; j++) {
								mPicList.add(pic[j]);
							}
						}
						OldShareEntity oldShareEntity = new OldShareEntity(
								evaluate_id, title, content, mPicList, phone,
								goodsName, time);
						oldShareAdapter.append(oldShareEntity);
					}
				} else {
					mIsEmpty = true; // 空数据
					mHasMore = false; // 无更多数据
				}
				mLoadMoreListViewContainer.loadMoreFinish(mIsEmpty, mHasMore); // 判断是否加载更多
			}
		}

		@Override
		public void onRequestPrepare() {
			// TODO Auto-generated method stub
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			// TODO Auto-generated method stub
			super.onRequestFailed(error);

		}
	}
}
